//
//  ST_EditDailyMomentVideoItemView.swift
//  btvideo
//
//  Created by mac on 2020/7/22.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SDWebImage

class ST_EditDailyMomentVideoItemView: UIControl {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.cornerRadius = 10
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(delete_btn)
        delete_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(17+5)
            make.right.top.equalToSuperview()
        }
        
        self.addSubview(gradient_view)
        gradient_view.snp.makeConstraints { (make) in
            make.bottom.right.left.equalToSuperview()
            make.height.equalTo(26)
        }
        
        self.addSubview(sign_image_view)
        sign_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.left.equalToSuperview().offset(5)
            make.bottom.equalToSuperview().offset(-5)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.right.equalToSuperview().offset(-5)
            make.bottom.equalToSuperview().offset(-5)
        }
        
        
        self.addTarget(self, action: #selector(preview_handle), for: UIControl.Event.touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var cover_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          control.cornerRadius = 10
          return control
      }()
 
    fileprivate lazy var sign_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("video_sign_small"))
        return control
    }()
    
    
    fileprivate lazy var delete_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("media_delete_small")!)
        control.addTarget(self, action: #selector(delete_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = .white
        control.font = UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.medium)
        return control
    }()

    
    
    fileprivate lazy var gradient_view:ST_FWGradientView = {
            
        let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor.init(white: 1, alpha: 0).cgColor,UIColor.init(white: 1, alpha: 0.3).cgColor]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        return control
    }()
    
    
    var thumbnail_image_path:String?
    
    var videoURL:URL?{
        didSet{
            
            guard let url = videoURL else{
                return
            }
                
            guard let img = url.get_video_thumbnail_image(),let imgPath = img.save_to_b_t_temporary_directory() else{
                return
            }
        
            thumbnail_image_path = imgPath
            
            let duration = AVURLAsset(url: url).duration.seconds

            cover_image_view.image = img
            
            let minutes = (duration/60).int
            
            var durationStr = ""
            
            if minutes > 0{
                durationStr = minutes.string + ":" + (duration.int % 60).string
            }else{
                durationStr = String.init(format: "00:%02d", duration.int)
            }
            
            content_label.text = durationStr
                
        }
    }
    
    
    var clear_closure:(()->())?
}


extension ST_EditDailyMomentVideoItemView{
    
    @objc fileprivate func preview_handle(){
        
        guard let url = self.videoURL else { return  }
        
        let playCtr = ST_BTPlayerViewController()
        
        playCtr.show_delete_action = false
        
        playCtr.type = .localUrl
        
        playCtr.videoUrl = url.path
        
        GlobalPresent(toController: playCtr)
    }
    
    @objc fileprivate func delete_handle(){
        
        clear_closure?()
    }
}






